<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class TasksController extends AppController {
	
	public $helpers = array('Html', 'Form');
	public $uses = array('Task','Client');
	
/*	public $paginate = array(
        'limit' => 20,
        'order' => array('Task.created' => 'desc')
		);*/
	
	public function index($client_id){
		
		if($client_id==''){
			$this->redirect(array('controller' => 'clients', 'action' => 'index', $client_id));
		}
		
		$this->request->data['Task']['client_id'] = $client_id; //Added this line
		$this->set('tasks', $this->Task->find('all', array('conditions' => array('Task.client_id =' => $client_id, 'Task.department_id =' => $this->Auth->user('department_id')))));
		
		
		$this->set('client_id',$client_id);
		$this->set('clients', $this->Client->find('all'));//get the department from the departments table
	}
	
	public function view($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid client'));
        }

        $client = $this->Client->findById($id);
        if (!$client) {
            throw new NotFoundException(__('Invalid client'));
        }
        $this->set('client', $client);
    }
	
	public function add($client_id) {
		if ($this->request->is('post')) {
			$this->request->data['Task']['department_id'] = $this->Auth->user('department_id'); //Added this line
			$this->request->data['Task']['created_by'] = $this->Auth->user('id'); //Added this line
			$this->request->data['Task']['client_id'] = $client_id; //Added this line
			if ($this->Task->save($this->request->data)) {
				$this->Session->setFlash('Your task has been saved.');
				$this->redirect(array('action' => 'index', $client_id));
			}
		}
		$this->set('client_id',$client_id);
		$this->set('clients', $this->Client->find('all'));//get the department from the departments table

	}
	
	public function edit($client_id,$id = null) {
		if (!$id) {
			throw new NotFoundException(__('Invalid task'));
		}
	
		$task = $this->Task->findById($id);
		if (!$task) {
			throw new NotFoundException(__('Invalid task'));
		}
	
		if ($this->request->is('task') || $this->request->is('put')) {
			$this->Task->id = $id;
			if ($this->Task->save($this->request->data)) {
				$this->Session->setFlash('Your task has been updated.');
				$this->redirect(array('action' => 'index', $client_id));
			} else {
				$this->Session->setFlash('Unable to update your task.');
			}
		}
	
		if (!$this->request->data) {
			$this->request->data = $task;
		}
		
		$this->set('client_id',$client_id);
		$this->set('clients', $this->Client->find('all'));//get the department from the departments table
	}
	
	public function delete($id,$client_id) {
		if ($this->request->is('get')) {
			throw new MethodNotAllowedException();
		}
	
		if ($this->Task->delete($id)) {
			$this->Session->setFlash('The task with id: ' . $id . ' has been deleted.');
			$this->redirect(array('controller' => 'tasks', 'action' => 'index', $client_id));
		}
	}
}
